#include "mainwindow.h"
#include <QSizePolicy>
#include <QDebug>
#include <QFileDialog>
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    initUi();
    mJsonFile = new JsonFile();

    connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::slotOpenFile);
    connect(ui->actionPan, &QAction::triggered, this, &MainWindow::slotPan);
    connect(ui->actionVirtualWall, &QAction::triggered, this, &MainWindow::slotVirtualWall);
    connect(ui->actionForbiddenZone, &QAction::triggered, this, &MainWindow::slotForbiddenZone);
    connect(ui->actionDefaultView2D, &QAction::triggered, this, &MainWindow::slotDefautView2D);


    //emit virturalwall
    connect(image_viewer_,&QViewImage::emitSendData, this,&MainWindow::slotRecvData);
    connect(image_viewer_,&QViewImage::emitSaveData, this,&MainWindow::slotSaveData);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initUi()
{
    ui->setupUi(this);
    m_splitter_main_ = new QSplitter(Qt::Horizontal);
    this->setCentralWidget(m_splitter_main_);

    point_cloud_viewer_ = new ViewWindow();
    image_viewer_ = new QViewImage();

    m_splitter_main_->addWidget(point_cloud_viewer_);
    m_splitter_main_->addWidget(image_viewer_);

    QList<int> widgetSizes;
    widgetSizes << 500 << 500;
    m_splitter_main_->setSizes(widgetSizes);

}

void MainWindow::slotOpenFile()
{
    //open dir
    QString appPath = QCoreApplication::applicationDirPath();
    qDebug()<<appPath;

    QString path = QFileDialog::getExistingDirectory(this,
                                                     tr("choose dir"),
                                                     appPath
                                                     );
    mFilePath = path;

    qDebug()<< path;

    //check file
    QString fullPath = "";
    if(!getExistFileTypeName(path, ".png", fullPath)){
        QMessageBox::information(this, "file", "The file isn't exist!");
        return;
    }

    //display
    qDebug()<<"display:"<<fullPath;

    image_viewer_->setImage(fullPath);
    point_cloud_viewer_->setPath(path);

    // read param
    QString jsonFilePath = "";
    if(getExistFileTypeName(path, ".json", jsonFilePath)){
        //read param
        QJsonObject obj;
        if(mJsonFile->readJsonFromFile(jsonFilePath, obj))
            image_viewer_->setVirtualWalls(obj);

    }else{
        qDebug()<<"json isn't exist.";
    }
}

bool MainWindow::getExistFileTypeName(const QString path, const QString type, QString& fullFileName)
{
    QDir dirs(path);
    QFileInfoList fileList = dirs.entryInfoList();
    bool ret =false;
    for(int i = 0; i < fileList.size(); i++)
    {
        qDebug()<<fileList[i].fileName();
        if(fileList[i].fileName().right(type.size()) == type){
            ret |= true;
            fullFileName = path + "/" + fileList[i].fileName();
        }
    }

    return ret;
}

void MainWindow::slotPan()
{
    image_viewer_->setPainterToolType(PAINTER_TOOL_TYPE::PAN);
}

void MainWindow::slotVirtualWall()
{
    image_viewer_->setPainterToolType(PAINTER_TOOL_TYPE::ADD_VIRTUAL_WALL);
}


void MainWindow::slotForbiddenZone()
{
    image_viewer_->setPainterToolType(PAINTER_TOOL_TYPE::ADD_FORBIDDEN_ZONE);
}

void MainWindow::slotDefautView2D()
{
    image_viewer_->zoomToFullExtent();
}

void MainWindow::slotRecvData(QVector<QVector<QPointF>> vec,int w,int h)
{
    qDebug() << "recvData ok!!!";
    qDebug() << "width:"<<w<<","<<"height:"<<h;
    if(!vec.isEmpty()){

        //todo
        int len = vec.size();
        for(int i = 0; i < len; i++){
            qDebug()<<"vec["<<i<<"]:["<<vec[i][0].x()<<","<<vec[i][0].y()<<"]";
            qDebug()<<"vec["<<i<<"]:["<<vec[i][1].x()<<","<<vec[i][1].y()<<"]";
        }
        point_cloud_viewer_->setCurrentHeight(h);
        point_cloud_viewer_->setPoints(vec);

        return;
    }
    qDebug() << "recvData is null.";

    return;
}

void MainWindow::slotSaveData()
{
    qDebug() << "SaveData slot!!!";

    QVector<QVector<QPointF>> vec;
    image_viewer_->getVirtualWalls(vec);
    if(vec.isEmpty())
        return;
    int len = vec.size();
    for(int i = 0; i < len; i++){
        qDebug()<<"vec["<<i<<"]:["<<vec[i][0].x()<<","<<vec[i][0].y()<<"]";
        qDebug()<<"vec["<<i<<"]:["<<vec[i][1].x()<<","<<vec[i][1].y()<<"]";
    }
    QString str = mFilePath + "/config.json";

    mJsonFile->creatJsonFile(str, vec);
    return;
}
